package com.luwanglin.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luwanglin.eduservice.entity.EduSubject;
import com.luwanglin.eduservice.entity.excel.ExcelSubjectData;
import com.luwanglin.eduservice.entity.subject.SubjectNestedVo;
import com.luwanglin.eduservice.entity.subject.SubjectNode;
import com.luwanglin.eduservice.entity.subject.SubjectVo;
import com.luwanglin.eduservice.listener.SubjectExcelListener;
import com.luwanglin.eduservice.mapper.EduSubjectMapper;
import com.luwanglin.eduservice.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-10
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ExcelSubjectData.class, new SubjectExcelListener(this)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<SubjectNestedVo> nestedList() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
//        wrapper.orderByAsc("sort");
        List<EduSubject> subjectOneList = baseMapper.selectList(wrapper);

        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", "0");
        List<EduSubject> childrenSubjectList = baseMapper.selectList(wrapper2);
        ArrayList<SubjectNestedVo> resultList = new ArrayList<>();

        for (EduSubject eduSubjectOne : subjectOneList) {
            SubjectNestedVo subjectNestedVo = new SubjectNestedVo();
            BeanUtils.copyProperties(eduSubjectOne, subjectNestedVo);
            resultList.add(subjectNestedVo);

            //填充二级分类vo数据
            for (EduSubject subjectVo : childrenSubjectList) {

                if (eduSubjectOne.getId().equals(subjectVo.getParentId())) {

                    //创建二级类别vo对象
                    SubjectVo tSubject = new SubjectVo();
                    BeanUtils.copyProperties(subjectVo, tSubject);
                    subjectNestedVo.getChildren().add(tSubject);

                }
            }
        }
        return resultList;
    }

    //获取课程树
    @Override
    public List<SubjectNode> getSubjectTree() {
        //课程树
        List<SubjectNode> subjectTree = new ArrayList<>();
        //课程数据
        List<EduSubject> eduSubjects = baseMapper.selectList(null);
        //使用map进行降低复杂度

        //循环取出一条数据
        for (EduSubject item : eduSubjects) {
            //获取根节点
            if ("0".equals(item.getParentId())) {
                SubjectNode node = new SubjectNode();
                node.setId(item.getId());
                node.setTitle(item.getTitle());
                //通过递归发放添加子节点
                node.setChildren(getChildren(node, eduSubjects));
                //将节点封装到课程树种
                subjectTree.add(node);
            }
        }
        return subjectTree;
    }

    //获取子节点
    private List<SubjectNode> getChildren(SubjectNode node, List<EduSubject> eduSubjects) {
        List<SubjectNode> nodeTree = new ArrayList<>();

        for (EduSubject item : eduSubjects) {
            //判断循环的数据是否为当前节点的子节点
            if (item.getParentId().equals(node.getId())) {
                SubjectNode childrenNode = new SubjectNode();
                childrenNode.setId(item.getId());
                childrenNode.setTitle(item.getTitle());
                //通过递归为当前子节点添加子节点
                childrenNode.setChildren(getChildren(childrenNode, eduSubjects));
                nodeTree.add(childrenNode);
            }
        }
        return nodeTree;
    }


    @Override
    public List<SubjectNode> nestedListByMap() {
//        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id", "0");
//        wrapper.orderByAsc("sort", "parent_id");
        List<EduSubject> allData = baseMapper.selectList(null);
        HashMap<String, List<EduSubject>> parentToChildren = new HashMap<>();
        for (EduSubject subject : allData) {
            if (parentToChildren.containsKey(subject.getParentId())) {
                List<EduSubject> eduSubjects = parentToChildren.get(subject.getParentId());
                eduSubjects.add(subject);
            } else {
                List<EduSubject> eduSubjects = new ArrayList<>();
                eduSubjects.add(subject);
                parentToChildren.put(subject.getParentId(), eduSubjects);
            }
        }
        List<SubjectNode> result = new ArrayList<>();
        for (EduSubject subject : parentToChildren.get("0")) {
            SubjectNode subjectNode = new SubjectNode();
            BeanUtils.copyProperties(subject, subjectNode);
            result.add(subjectNode);
            recursionFindChildren(parentToChildren, subjectNode);
        }
        return result;

    }

    public void recursionFindChildren(HashMap<String, List<EduSubject>> subjectNodeList, SubjectNode cur) {
        List<SubjectNode> children = cur.getChildren();
        List<EduSubject> curChildren = subjectNodeList.get(cur.getId());
        //注意这里要进行判断是否为空，否则后续的遍历会出错
        if (curChildren == null) {
            return;
        }
        for (EduSubject subject : curChildren) {
            SubjectNode subjectNode = new SubjectNode();
            BeanUtils.copyProperties(subject, subjectNode);
            children.add(subjectNode);
            recursionFindChildren(subjectNodeList, subjectNode);
        }
    }


}
